<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2017/8/26
 * Time: 18:40
 */
namespace Home\Controller;
use Think\Controller;

class PersonageController extends Controller{
    public function index(){
        if(empty($_SESSION['users'])){
            if($_GET['cid']&&$_GET['qid']){
                $_SESSION['id_arr']=['cid'=>$_GET['cid'],'qid'=>$_GET['qid'],'type'=>$_GET['type']];
            }
            $this->display("Login:index");
        }else{
            $data = M("order")->where("users_id={$_SESSION['users']['id']} and ord_status=0 and cancel=0")->select();//未付款运单
            $data1 = M("order")->where("users_id={$_SESSION['users']['id']} and ord_status=1")->select();//已付款运单
            $data2 = M("order")->where("users_id={$_SESSION['users']['id']}")->select();//已有运单
            $balance =$_SESSION['users']['balance'];
            foreach($data as $val){
                $balance-=$val['charge'];
            }
            $ac = count($data);
            $ac1 = count($data1);
            $ac2 = count($data2);
            $this->assign("balance",$balance);
            $this->assign("ac",$ac);
            $this->assign("ac1",$ac1);
            $this->assign("ac2",$ac2);
            $this->display();
        }
    }
    //查询下单
    public function tariff(){
        $scheme = M("weig_add")->alias("w")
            ->join("tb_starting s on s.id=w.start_id")
            ->join("tb_partition_country pc on pc.id=s.pcid")
            ->join("tb_channel_partition cp on cp.id=s.cpid")
            ->join("tb_partition p on p.id=pc.partition_id and p.id=cp.partition_id ")
            ->join("tb_channel q on q.id=cp.channel_id")
            ->join("tb_country c on c.id=pc.country_id")
            ->where("c.id=20")
            ->select();
        $g_pri=[];
        foreach($scheme as $key=>$val){
            foreach($val as $k=>$v){
                $g_pri[$k][$key]=$v;
            }
        }
        $post["weight"]=1;
        array_multisort($g_pri["stage_min_weig"], constant(""), $scheme);
        $arr=[];
        foreach($scheme as $value) {
            if ($post['weight'] >= $value['start_weight']) {
                if ($value['stage_min_weig'] <= $post['weight'] && $value['stage_max_weig'] >= $post['weight']) {
                    if($value['type']=0){//($get['weight'] - $value['start_weight']) % $value['add_weig']!=0
                        // $star = ceil(($get['weight'] - $value['start_weight']) / $value['add_weig']);
                        $yun = /*$star * $value['add_weig_price'] +*/ $value['start_fare'];
                    }else{
                        $yun = ceil(($post['weight'] - $value['start_weight']) / $value['add_weig']) * $value['add_weig_price'] + $value['start_fare'];
                    }
                    $value['price'] = $yun * ($value["fuel"]/100+1);
                    $value['weight'] = $post['weight'];
                    $arr[] = $value;
                }
            }elseif($value['stage_min_weig'] <= ceil($post['weight']) && $value['stage_max_weig'] >= ceil($post['weight'])){
                /*if(($get['weight'] - $value['start_weight']) % $value['add_weig']!=0){
                    $star = ($get['weight'] - $value['start_weight']) / $value['add_weig'];
                    $yun = $star * $value['add_weig_price'] + $value['start_fare'];*/
                if($value['type']=0){
                    $yun = $value['start_fare'];
                }else{
                    $yun = ceil(($post['weight'] - $value['start_weight']) / $value['add_weig']) * $value['add_weig_price'] + $value['start_fare'];
                }
                $value['price'] = $yun * ($value["fuel"]/100+1);
                $value['weight'] = $post['weight'];
                $arr[] = $value;
            }
        }
        $state=M("country")->where("pid=0")->order("`order` DESC")->select();
        $chan =M("channel")->order("company")->select();
        $this->assign("arr",$arr);
        $this->assign("sta",$state);
        $this->assign("chan",$chan);
        $this->display();
    }
    //运单信息
    public function order(){
        if(empty($_SESSION['users'])){
            echo "<script>alert('请登录后再操作');location.href='/login/index'</script>";
        }else{
            $get = I("get.");
            $data = M("order")->alias("o")
                ->join("tb_channel c on c.id=o.channel_id")
                    ->join("tb_recipients r on r.id=o.recipients_id")
                    ->join("tb_sender s on s.id=o.sender_id")
                    ->join("tb_country co on co.id=r.country_id")
                    ->join("tb_order_goods og on og.order_id=o.id")
                    ->join("tb_goods g on g.id=og.goods_id")
                ->where("o.users_id='{$_SESSION['users']['id']}'")->order("o.ord_time DESC,o.ord_status,o.cancel")->select();
            $this->assign("data",$data);
            $this->display();
          // echo  json_encode($data);
        }
    }
    public function ord_cancel(){
        $bol = M("order")->data(['cancel'=>1])->where("id={$_GET['nid']}")->save();
        if($bol){
            $this->success("订单已取消","/personage/order/num/".$_SESSION["users"]['id'],0);
        }
    }
    //下单操作
    public function waybill(){
        if(empty($_SESSION['users'])){
            echo "<script>alert('请登录后再操作');location.href='/login/index'</script>";
        }else {
            if (IS_POST) {
                $post = I("post.");
                //查价格后算运单价格
                $scheme = M("weig_add")->alias("w")
                    ->join("tb_starting s on s.id=w.start_id")
                    ->join("tb_partition_country pc on pc.id=s.pcid")
                    ->join("tb_channel_partition cp on cp.id=s.cpid")
                    ->join("tb_partition p on p.id=pc.partition_id and p.id=cp.partition_id ")
                    ->join("tb_channel q on q.id=cp.channel_id")
                    ->join("tb_country c on c.id=pc.country_id")
                    ->where("c.id={$post['country_id']} and q.id={$post['channel_id']}")
                    ->select();
                //进行重量段判断，算出价格
                foreach($scheme as $value) {
                    if ($post['weight'] >= $value['start_weight']) {
                        if ($value['stage_min_weig'] <= $post['weight'] && $value['stage_max_weig'] >= $post['weight']) {
                            if($value['type']=0){//($get['weight'] - $value['start_weight']) % $value['add_weig']!=0
                                // $star = ceil(($get['weight'] - $value['start_weight']) / $value['add_weig']);
                                $yun = /*$star * $value['add_weig_price'] +*/ $value['start_fare'];
                            }else{
                                $yun = ceil(($post['weight'] - $value['start_weight']) / $value['add_weig']) * $value['add_weig_price'] + $value['start_fare'];
                            }
                            $post['money'] = $yun +$yun * $value["fuel"]/100;
                        }
                    }elseif($value['stage_min_weig'] <= ceil($post['weight']) && $value['stage_max_weig'] >= ceil($post['weight'])){
                        if($value['type']=0){
                            $yun = $value['start_fare'];
                        }else{
                            $yun = ceil(($post['weight'] - $value['start_weight']) / $value['add_weig']) * $value['add_weig_price'] + $value['start_fare'];
                        }
                        $post['money'] = $yun +$yun * $value["fuel"]/100;
                    }
                }
                $post["charge"]=$post["money"];
                $post['ord_time'] = date("Y-m-d H:i:s", time());//运单生成时间
                $post['way'] = date("YmdHis",time());//生成运单号
                $senwhere=["users_id"=>$post['users_id'],'sender'=>$post['sender'], 'sen_tel'=>$post['sen_tel'],'email'=>$post['email'] , 'sen_add'=>$post['sen_add']];
                $sen=M("sender")->where("id={$post['s_id']}")->select();//查询寄件人是否已存在
                if($sen){
                    $post['sender_id']=$sen[0]['id'];//寄件人存在直接获取id
                    M("sender")->where("id={$post['s_id']}")->data($senwhere)->save();
                }else{
                    $post['sender_id']=M("sender")->data($post)->add();//不存在，插入并获取id
                }

                $recwhere = ["users_id"=>$post['users_id'],'recipients'=>$post['recipients'], 'rec_tel'=>$post['rec_tel'],'rec_comp'=>$post['rec_comp'] , 'rec_add'=>$post['rec_add'],'rec_postcode'=>$post['rec_postcode'],'country_id'=>$post['country_id'],'rec_province'=>$post['rec_province'],'rec_city'=>$post['rec_city']];
                $rec = M("recipients")->where("id={$post['s_id']}")->select();//查收件人是否存在
                if($rec){
                    $post['recipients_id']=$rec[0]['id'];//存在获取id
                    M("recipients")->where("id={$post['s_id']}")->data($recwhere)->save();
                }else{
                    $post['recipients_id']=M("recipients")->data($post)->add();//不存在，插入并获取id
                }
                foreach($post["g_cname"] as $k=>$v){
                    $gwhere []= ['g_cname'=>$v,'g_ename'=>$post['g_ename'][$k],'HS'=>$post['HS'][$k],"g_items"=>$post["g_items"][$k],"g_total_prices"=>$post["g_total_prices"][$k],"volume"=>$post["volume"][$k]];
                }
                foreach($gwhere as $wh){
                    $wh=['g_cname'=>$wh['g_cname'],'g_ename'=>$wh['g_ename'],'HS'=>$wh['HS']];
                    $good = M('goods')->where($wh)->select();
                    if($good){
                        $org[]=$good[0]['id'];
                    }else{
                        $org[]=M('goods')->data($wh)->add();
                    }
                }

                $bool = M("order")->data($post)->add();//插入下单数据

                foreach($org as $gid){
                    $idarr =["goods_id"=>$gid,'order_id'=>$bool];
                    M('order_goods')->data($idarr)->add();
                }

                if ($bool) {
                    $_SESSION['id_arr']=[];
                    $this->success("","/personage/ordtips/id/{$bool}",0);//下单成功
                }
            } else {//非post提交执行
                $state = M("country")->where("pid=0")->order("ename")->select();
                $channel = M("channel")->select();
                $this->assign("arr",$_GET);
                $this->assign("state",$state);
                $this->assign("channel", $channel);
                $this->display();
            }
        }
    }
    public function ordtips(){
        $ordno = M("order")->find("way")->where($_GET["id"])->select();
        $this->assign("ordno",$ordno["way"]);
        $this->display();
    }
    //财务充值
    public function recharge(){
        $this->display();
    }
    //财务明细
    public function detailed(){
        $get = I("get.");
        $data = M("order")->alias("o")
            ->join("tb_channel c on c.id=o.channel_id")
            ->join("tb_recipients r on r.id=o.recipients_id")
            ->join("tb_sender s on s.id=o.sender_id")
            ->join("tb_country co on co.id=r.country_id")
            ->join("tb_order_goods og on og.order_id=o.id")
            ->join("tb_goods g on g.id=og.goods_id")
            ->where("o.users_id='{$get['num']}'")->order("o.ord_time DESC,o.ord_status")->select();
        $money=0;
        foreach($data as $val){
            $money+=$val['money'];
        }
        $this->assign("money",$money);
        $this->assign("data",$data);
        $this->display();
    }
    //修改个人资料
    public function modifydata(){
        if(!IS_POST){
            $id=I("get.num");
            $user =M("users")->where("id={$id}")->select();
            $this->assign("modify",$user);
            $this->display();
        }else{
            $id=I("get.uid");
            $post=I("post.");
            $bol =M("users")->data($post)->where("id={$id}")->save();
            if($bol){
                $this->success("修改成功","/personage/message",1);
            }else{
                $this->error("修改失败");
            }
        }
    }

    //修改密码
    public function edit_pw(){
        if(!IS_POST){
            $this->display();
        }else{
            $pw = sha1(I("post.password"));
            $pw1 =sha1(I("post.password1"));
            if($pw==$pw1){
                $bol = M("users")->where("id={$_POST['uid']}")->save(["password"=>$pw]);
                if($bol){
                    $this->success("修改成功","/personage/message");
                }else{
                    $this->error("修改失败");
                }
            }
        }

    }
    //异步验证密码修改
    public function ajax_pw(){
        $pw = sha1($_GET['ori_pw']);
       $pass= M("users")->where("id={$_GET['id']}")->select();
        if($pass[0]['password']==$pw){
            echo 1;
        }else{
            echo 0;
        }
    }
    //账号信息
    public function message(){
        $data = M("order")->where("users_id={$_SESSION['users']['id']} and ord_status=0")->select();
        $data1 = M("order")->where("users_id={$_SESSION['users']['id']} and ord_status=1")->select();
        $data2 = M("order")->where("users_id={$_SESSION['users']['id']}")->select();
        $arr =M("users")->where("id=".$_SESSION['users']['id'])->select();
        $ac = count($data);
        $ac1 = count($data1);
        $ac2 = count($data2);
        $this->assign("ac",$ac);
        $this->assign("ac1",$ac1);
        $this->assign("ac2",$ac2);
        $this->assign("status",$data2);
        $this->assign("arr",$arr);
        $this->display();
    }
    //所有收件人信息
    public function rec_ajax(){
        $rec=M("recipients")->where("srec=1 and users_id={$_SESSION['users']['id']}")->select();
        echo json_encode($rec);
    }
    //所有发件人信息
    public function sen_ajax(){
        $rec=M("sender")->where("ssen=1 and users_id={$_SESSION['users']['id']}")->select();
        echo json_encode($rec);
    }
    //单个收件人信息
    public function rec_info(){
        $rec=M("recipients")->where("id={$_GET['id']}")->select();
        echo json_encode($rec);
    }
    //单个发件人信息
    public function sen_info(){
        $sen=M("sender")->where("id={$_GET['id']}")->select();
        echo json_encode($sen);
    }
    //空操作
    public function _empty(){
        $this->display("Public:404");
    }
}